//获取应用实例 
const app = getApp();

Page({
	data: {
    orderdetail:[],
    flag: 0,
    content:'',
    phone:'',
    linkman:'',

    /**
     * 图片
     */ 
    imgs: [],
    list: '',
    upload_picture_list: [],
    order_detail_id:'',
  },
	onLoad: function(options) {
    console.log(options.order_detail_id);
    this.setData({
      orderdetail:app.globalData.orderdetail,
      order_detail_id: options.order_detail_id
    })
  },


  contentInput:function(e){
    var that=this;
    that.setData({
      content:e.detail.value
    })
  },

  phoneInput: function (e) {
    var that = this;
    that.setData({
      phone: e.detail.value
    })
  },

  linkmanInput: function (e) {
    var that = this;
    that.setData({
      linkman: e.detail.value
    })
  },
  
  //留言 留言
  online:function(){
    var that = this;
    if (that.data.content.length == 0) {
      wx.showToast({
        title: '请填写您遇到的问题',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '数据提交中',
    })
   
    wx.uploadFile({
      url: app.globalData.api + '/index.php/wap/Message/online',
      filePath: that.data.upload_picture_list[0],
      name: 'img',
      formData: {//需要的参数
        token: app.globalData.token,
        cat_id: app.globalData.cat_idYj,
        linkman: that.data.linkman,
        phone: that.data.phone,
        content: that.data.content,
        'img': 'filePath',
      }, // HTTP 请求中其他额外的 form data
      header: {
        "Content-Type": "multipart/form-data"
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        var data = JSON.parse(res.data);
        if(data.code==1){
          wx.showToast({
            title: '上传成功',//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'success', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })

          setTimeout(function () {
            //要延时执行的代码
            wx.switchTab({
              url: '/pages/mine/mine',
            })
          }, 2000) //延迟时间 这里是1秒
           
        }else{
          wx.showToast({
            title: '' + data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
        
      }, fail: function (res) {
          console.log(res)
          wx.hideLoading();
      }, complete: function(res){
        // console.log(res)
       
      }
    });
  },

  //选择图片方法
  uploadpic: function (e) {
    let that = this //获取上下文
    let upload_picture_list = that.data.upload_picture_list
    //选择图片
    wx.chooseImage({
      count: 8, // 默认9，这里显示一次选择相册的图片数量 
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) { // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
      console.log(res)
        let tempFiles = res.tempFilePaths
        //把选择的图片 添加到集合里
        for (let i in tempFiles) {
          // tempFiles[i]['upload_percent'] = 0
          // tempFiles[i]['path_server'] = ''
          upload_picture_list.push(tempFiles[i])
        }
        //显示
        that.setData({
          upload_picture_list: upload_picture_list,
        });

        console.log(that.data.upload_picture_list);
      }
    })
  },
  //点击上传图片
  uploadimage() {
    let page = this
    let upload_picture_list = page.data.upload_picture_list
    console.log(upload_picture_list[0])
    console.log(upload_picture_list.length)
    for (let i = 0; i < upload_picture_list.length; i++) {
      wx.uploadFile({
        url: app.globalData.api + '/index.php/wap/Order/comment', //此处换上你的接口地址 
        filePath: upload_picture_list[i],
        name: 'img',
        header: {
          "Content-Type": "multipart/form-data",
        },
        formData: {
          'token': app.globalData.token,
          'order_detail_id': page.data.order_detail_id,
          'score': page.data.flag,
          'grade': 1,
          'content': page.data.content,
          'img':'filePath',
        }, // HTTP 请求中其他额外的 form data
        success: function (res) {
          console.log(res);
          let data = JSON.parse(res.data); // 这个很关键
        },fail:function(res){
          console.log(res)
        }
      })
    }  
    let imgs = wx.setStorageSync('imgs', upload_picture_list);
  },
  // 点击删除图片
  deleteImg(e) {
    let upload_picture_list = this.data.upload_picture_list;
    let index = e.currentTarget.dataset.index;
    upload_picture_list.splice(index, 1);
    this.setData({
      upload_picture_list: upload_picture_list
    });
    console.log(this.data.upload_picture_list)
  },
  // 预览图片
  previewImg(e) {
    //获取当前图片的下标
    let index = e.currentTarget.dataset.index;
    //所有图片
    let imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});


/**
 * 上传图片方法
 */
function upload_file_server(url, that, upload_picture_list, j) {
  console.log(url)
  console.log(upload_picture_list)
  console.log(upload_picture_list[j]['path'])
  //上传返回值
  const upload_task = wx.uploadFile({
    // 模拟https
    url: url, //需要用HTTPS，同时在微信公众平台后台添加服务器地址  
    filePath: upload_picture_list[j]['path'], //上传的文件本地地址    
    name: 'img',
    formData: {
      token:app.globalData.token,
      order_detail_id: that.data.order_detail_id,
      score: that.data.flag,
      grade: 1,
      content: that.data.content,
      // img:'filePath',
    }, // HTTP 请求中其他额外的 form data
    header: {
      "Content-Type": "multipart/form-data"
    },
    //附近数据，这里为路径     
    success: function (res) {
      console.log(res)
      var data = JSON.parse(res.data);
      // //字符串转化为JSON  
      console.log(data)
      // if (data.Success == true) {
      //   var filename = data.file //存储地址 显示
      //   upload_picture_list[j]['path_server'] = filename
      // } else {
      //   upload_picture_list[j]['path_server'] = filename
      // }
      // that.setData({
      //   upload_picture_list: upload_picture_list
      // });
      wx.setStorageSync('imgs', upload_picture_list);
    }
  })
  //上传 进度方法
  upload_task.onProgressUpdate((res) => {
    upload_picture_list[j]['upload_percent'] = res.progress
    console.log(res.progress)
    that.setData({
      upload_picture_list: upload_picture_list
    });
    console.log(that.data.upload_picture_list);
  });
}